/*************************************************
 
    Results: Treatment Effects on Worker Interactions
 
*************************************************/

*-------------------------------------------------------------
* Set Memory and Environment
*-------------------------------------------------------------
set more off
clear all
set segmentsize 3g
set scheme plotplainblind


*-------------------------------------------------------------
* Define Local Macros for Analysis Options
*-------------------------------------------------------------
local main_regressions          1
local RI_Test                   1
local line_clustering           1

*-------------------------------------------------------------
* Main Regressions
*-------------------------------------------------------------
if `main_regressions' == 1 {
    
    *******************************
    ****** Main Regressions *******
    *******************************
    
    * Load Cleaned Data
    use "$Data/Final/worker_choices.dta", clear
    
    * Define Conditions and Corresponding Labels
    local conditions Free Blame Choice_R
    local labels "Identified teammate as Feee Rider" "Blamed by teammate" "Unwilling to give up Relief Time"
    
    * Initialize Lists for estout
    local estout_models ""
    
    * Loop Through Each Condition
    foreach cond in `conditions' {
        
        * Determine Estimation Prefix Based on Condition
        local est_prefix ""
        if "`cond'" == "Free" {
            local est_prefix "A1 A2"
        }
        else if "`cond'" == "Blame" {
            local est_prefix "A3 A4"
        }
        else if "`cond'" == "Choice_R" {
            local est_prefix "A5 A6"
        }
        
        * Run Regressions for Current Condition
        
        * Regression 1: Without Interaction
        areg chosen mixed i.skill i.religion if `cond', absorb(line_section) cluster(line_sec_team)
        eststo `=word("`est_prefix'",1)'
        local obs`=word("`est_prefix'",1)' = e(N)
        su chosen if e(sample) == 1
        local mean`=word("`est_prefix'",1)' = string(round(r(mean), .01))
		local r2`=word("`est_prefix'",1)' = string(round(e(r2_a), 0.001), "%9.3f")
        
        * Regression 2: With Interaction
        areg chosen c.mixed#i.dep i.skill i.religion if `cond', absorb(line_section) cluster(line_sec_team)
        eststo `=word("`est_prefix'",2)'
        local obs`=word("`est_prefix'",2)' = e(N)
		local r2`=word("`est_prefix'",2)' = string(round(e(r2_a), 0.001), "%9.3f")
        test 1.dep#c.mixed = 0.dep#c.mixed
        local p`=word("`est_prefix'",2)' = string(round(r(p), .01))
        
        * Append Models to estout_models List
        local estout_models "`estout_models' `=word("`est_prefix'",1)' `=word("`est_prefix'",2)'"
    }
    
    * Export Results to LaTeX
    estout `estout_models' using "$Output/Tables/tables_wchoices1", style(tex) replace ///
        keep(mixed 1.dep#c.mixed 0.dep#c.mixed) ///
        cells(b(star fmt(%9.4f)) se(par)) ///
        nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
        varlabels(mixed "Mixed" ///
            0.dep#c.mixed "Mixed X LD" /// 
            1.dep#c.mixed "Mixed X HD")
    
    * Create LaTeX Footer
    local tex " \\ \hline"
    local tex "`tex'p(Mixed X HD = Mixed X LD) &  & `pA2' &  & `pA4' & & `pA6'  \\"
    local tex "`tex' Mean Dep. Var & `meanA1' & `meanA1' & `meanA3' & `meanA3' & `meanA5' & `meanA5'   \\"
    local tex "`tex' Worker Skill F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
    local tex "`tex' Religion F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
    local tex "`tex' Line*Section Effects & Yes & Yes & Yes & Yes  \\"
    local tex "`tex' Observations & `obsA1' & `obsA2' & `obsA3' & `obsA4'  & `obsA5' & `obsA6'   \\"
	local tex "`tex' Adj. $ R^2$ & `r2A1' & `r2A2' & `r2A3' & `r2A4' & `r2A5' & `r2A6' \\"
    local tex "`tex' \multicolumn{7}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at line-section-team level."
    local tex "`tex' Mixed is a dummy variable coded 1 if the line-section-level team is religiously mixed." 
    local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
    
    * Append Footer to LaTeX Table
    esttab `estout_models' using "$Output/Tables/tables_wchoices1a", style(tex) replace booktabs ///
        d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
        mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") ///
        mgroups("Identified teammate as Feee Rider" "Blamed by teammate" "Unwilling to give up Relief Time", ///
                pattern(1 0 1 0 1 0) ///
                prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
    
    *-------------------------------------------------------------
    * Main Table (Overall Effect but only Hindus) -- Table B.11
    *-------------------------------------------------------------
    preserve
    keep if religion == 1
    
    * Reuse Conditions and Labels
    local estout_models_hindu ""
    
    * Loop Through Each Condition for Hindus
    foreach cond in `conditions' {
        
        * Determine Estimation Prefix Based on Condition
        local est_prefix ""
        if "`cond'" == "Free" {
            local est_prefix "A1 A2"
        }
        else if "`cond'" == "Blame" {
            local est_prefix "A3 A4"
        }
        else if "`cond'" == "Choice_R" {
            local est_prefix "A5 A6"
        }
        
        * Run Regressions for Current Condition
        
        * Regression 1: Without Interaction
        areg chosen mixed i.skill i.religion if `cond', absorb(line_section) cluster(line_sec_team)
        eststo `=word("`est_prefix'",1)'
        local obs`=word("`est_prefix'",1)' = e(N)
		local r2`=word("`est_prefix'",1)' = string(round(e(r2_a), 0.001), "%9.3f")
        su chosen if e(sample) == 1
        local mean`=word("`est_prefix'",1)' = string(round(r(mean), .01))
        
        * Regression 2: With Interaction
        areg chosen c.mixed#i.dep i.skill i.religion if `cond', absorb(line_section) cluster(line_sec_team)
        eststo `=word("`est_prefix'",2)'
        local obs`=word("`est_prefix'",2)' = e(N)
		local r2`=word("`est_prefix'",2)' = string(round(e(r2_a), 0.001), "%9.3f")
        test 1.dep#c.mixed = 0.dep#c.mixed
        local p`=word("`est_prefix'",2)' = string(round(r(p), .001))
        
        * Append Models to estout_models_hindu List
        local estout_models_hindu "`estout_models_hindu' `=word("`est_prefix'",1)' `=word("`est_prefix'",2)'"
    }
    
    * Export Results to LaTeX for Hindus
    estout `estout_models_hindu' using "$Output/Tables/tables_wchoicesh", style(tex) replace ///
        keep(mixed 1.dep#c.mixed 0.dep#c.mixed) ///
        cells(b(star fmt(%9.4f)) se(par)) ///
        nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
         varlabels(mixed "Mixed" ///
            0.dep#c.mixed "Mixed X LD" /// 
            1.dep#c.mixed "Mixed X HD")
    
    * Create LaTeX Footer for Hindus
    local tex_hindu " \\ \hline"
    local tex_hindu "`tex_hindu'p(Mixed X HD = Mixed X LD) &  & `pA2' &  & `pA4' & & `pA6'  \\"
    local tex_hindu "`tex_hindu' Mean Dep. Var & `meanA1' & `meanA1' & `meanA3' & `meanA3' & `meanA5' & `meanA5'   \\"
    local tex_hindu "`tex_hindu' Worker Skill F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
    local tex_hindu "`tex_hindu' Religion F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
    local tex_hindu "`tex_hindu' Line*Section Effects & Yes & Yes & Yes & Yes  \\"
    local tex_hindu "`tex_hindu' Observations & `obsA1' & `obsA2' & `obsA3' & `obsA4'  & `obsA5' & `obsA6'   \\"
	local tex_hindu "`tex_hindu' Adj. $ R^2$ & `r2A1' & `r2A2' & `r2A3' & `r2A4' & `r2A5' & `r2A6' \\"
    local tex_hindu "`tex_hindu' \multicolumn{7}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at line-section-team level."
    local tex_hindu "`tex_hindu' Mixed is a dummy variable coded 1 if the line-section-level team is religiously mixed." 
    local tex_hindu "`tex_hindu' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
    
    * Append Footer to LaTeX Table for Hindus
    esttab `estout_models_hindu' using "$Output/Tables/tables_wchoicesha", style(tex) replace booktabs ///
        d(*) nolabel collabels(none) noobs postfoot("`tex_hindu'") nonum ///
        mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") ///
        mgroups("Identified teammate as Feee Rider" "Blamed by teammate" "Unwilling to give up Relief Time", ///
                pattern(1 0 1 0 1 0) ///
                prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
				
restore
    
    
}
    
    



*-------------------------------------------------------------
* Randomization Inference Analysis
*-------------------------------------------------------------
if `RI_Test' == 1{


* set path

use "$Data/Final/worker_choices.dta", clear


* Create a single variable for treatment for randomization inference.
gen treatment = 0
replace treatment = 1 if mixedXLD == 1
replace treatment = 2 if mixedXHD == 1

* Placeholder for p-values
local pvalues ""

* Loop through each dependent variable
foreach scenario in Free Blame Choice_R {
	
	local modelname = "chosen_`scenario'_model"

	****** Regression with Mixed Dummy ******
	{
	* Run the regression
    reghdfe chosen i.mixed i.skill  i.religion if `scenario', absorb(line_section) cluster(line_sec_team) 
	
    * Store the regression result
	estimates store `modelname'_mixed
	
	* Extract coefficients and standard errors, then round to three decimal places
	local mixed_coef_`scenario' = string(round(e(b)[1,"1.mixed"], 0.001), "%9.3f")
	
	local obs`scenario'_mixed = e(N)
	su chosen if e(sample) == 1
	local mean`scenario'_mixed = string(round(r(mean), .01), "%9.2f")
    
	* Calculate p-values and append significance stars
	local mixed_t_`scenario' = e(b)[1,"1.mixed"]/sqrt(e(V)[2,2])
	local mixed_pval_`scenario' = string(round(2*ttail(e(df_r),abs(`mixed_t_`scenario'')), 0.001), "%9.3f")
	local mixed_stars_`scenario' = cond(`mixed_pval_`scenario'' < 0.01, "***", cond(`mixed_pval_`scenario'' < 0.05, "**", cond(`mixed_pval_`scenario'' < 0.10, "*", "")))

	* Combine coefficients and stars
	local mixed_result_`scenario' = "`mixed_coef_`scenario''`mixed_stars_`scenario''"

	
    * Run ritest (simplified command for demonstration)
    ritest mixed _b[1.mixed],  samplingsourcefile("$Data/Original/Randomized_Teams_RI.dta")  samplingmatchvar(_id) reps(2000) nodots: ///
    reghdfe chosen i.mixed i.skill  i.religion if `scenario', absorb(line_section) cluster(line_sec_team)
    
	local mixed_ri_`scenario' = string(round(el(r(p), 1, 1), 0.001), "%9.3f")
	}
	
    ****** Regression with Mixed Interaction ******
	{
	* Run the regression
    reghdfe chosen i.treatment i.skill  i.religion if `scenario', absorb(line_section) cluster(line_sec_team)
    estimates store `modelname'
	
	* Extract coefficients and standard errors, then round to three decimal places
	local mixedXLD_coef_`scenario' = string(round(e(b)[1,"1.treatment"], 0.001), "%9.3f")
	local mixedXHD_coef_`scenario' = string(round(e(b)[1,"2.treatment"], 0.001), "%9.3f")
	local obs`scenario' = e(N)
	su chosen if e(sample) == 1
	local mean`scenario' = string(round(r(mean), .01), "%9.2f")
      
	* Calculate p-values and append significance stars
	local mixedXLD_t_`scenario' = e(b)[1,"1.treatment"]/sqrt(e(V)[2,2])
	local mixedXLD_pval_`scenario' = string(round(2*ttail(e(df_r),abs(`mixedXLD_t_`scenario'')), 0.001), "%9.3f")
	local mixedXLD_stars_`scenario' = cond(`mixedXLD_pval_`scenario'' < 0.01, "***", cond(`mixedXLD_pval_`scenario'' < 0.05, "**", cond(`mixedXLD_pval_`scenario'' < 0.10, "*", "")))
	local mixedXHD_t_`scenario' = e(b)[1,"2.treatment"]/sqrt(e(V)[3,3])
	local mixedXHD_pval_`scenario' = string(round(2*ttail(e(df_r),abs(`mixedXHD_t_`scenario'')), 0.001), "%9.3f")
	local mixedXHD_stars_`scenario' = cond(`mixedXHD_pval_`scenario'' < 0.01, "***", cond(`mixedXHD_pval_`scenario'' < 0.05, "**", cond(`mixedXHD_pval_`scenario'' < 0.10, "*", "")))

	* Combine coefficients and stars
	local mixedXLD_result_`scenario' = "`mixedXLD_coef_`scenario''`mixedXLD_stars_`scenario''"
	local mixedXHD_result_`scenario' = "`mixedXHD_coef_`scenario''`mixedXHD_stars_`scenario''"
	
    * Run ritest (simplified command for demonstration)
    ritest treatment _b[1.treatment] _b[2.treatment],  samplingsourcefile("$Data/Original/Randomized_Teams_RI.dta")  samplingmatchvar(_id) reps(2000) nodots: ///
    reghdfe chosen i.treatment i.skill  i.religion if `scenario', absorb(line_section) cluster(line_sec_team)
    
	local mixedXLD_ri_`scenario' = string(round(el(r(p), 1, 1), 0.001), "%9.3f")
	local mixedXHD_ri_`scenario' = string(round(el(r(p), 1, 2), 0.001), "%9.3f")
	}
}

* Note: This assumes `ritest` successfully stores p-value in r(p) after each run
* Replace `ritest` command with your actual ritest setup

local models "chosen_Free_model_mixed chosen_Free_model chosen_Blame_model_mixed chosen_Blame_model chosen_Choice_R_model_mixed chosen_Choice_R_model"

* Open a .tex file to store the results
file open results using $Output/Tables/tables_ritest_alternative.tex, write replace

* Write the results to the .tex file
file write results "Mixed & `mixed_result_Free' &	& `mixed_result_Blame' &	& `mixed_result_Choice_R' &	 \\" _n
file write results "	& (`mixed_pval_Free') &	& (`mixed_pval_Blame') &	& (`mixed_pval_Choice_R') &	 \\" _n
file write results "	& [`mixed_ri_Free'] &	& [`mixed_ri_Blame'] &	& [`mixed_ri_Choice_R'] &	 \\" _n
file write results "Mixed $\times$ LD &	& `mixedXLD_result_Free' &	& `mixedXLD_result_Blame' &	& `mixedXLD_result_Choice_R'  \\" _n
file write results "	&	& (`mixedXLD_pval_Free') &	& (`mixedXLD_pval_Blame') &	& (`mixedXLD_pval_Choice_R')  \\" _n
file write results "	&	& [`mixedXLD_ri_Free'] &	& [`mixedXLD_ri_Blame'] &	& [`mixedXLD_ri_Choice_R'] \\" _n
file write results "Mixed $\times$ HD &	& `mixedXHD_result_Free' &	& `mixedXHD_result_Blame' &	& `mixedXHD_result_Choice_R'  \\" _n
file write results "	&	& (`mixedXHD_pval_Free') &	& (`mixedXHD_pval_Blame') &	& (`mixedXHD_pval_Choice_R')  \\" _n
file write results "	&	& [`mixedXHD_ri_Free'] &	& [`mixedXHD_ri_Blame'] &	& [`mixedXHD_ri_Choice_R'] \\" _n

file close results


local tex " \\ \hline"
local tex "`tex' Worker Skill F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Religion F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Line $\times$ Section F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Mean Dep Var. & `meanFree_mixed' & `meanFree' & `meanBlame_mixed' & `meanBlame' & `meanChoice_R_mixed' & `meanChoice_R' \\"
local tex "`tex' N & `obsFree_mixed' & `obsFree' & `obsBlame_mixed' & `obsBlame' & `obsChoice_R_mixed' & `obsChoice_R' \\"
local tex "`tex' \multicolumn{7}{p{16cm}}{\tiny \textit{Notes:} Standard errors clustered at line-section-team level."
local tex "`tex' P-values are reported in parentheses."
local tex "`tex' Mixed  is  a  dummy  variable  coded  1  if  the  line-section-level  team  is  religiously mixed." 
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"

esttab `models' using "$Output/Tables/tables_ritest_alternativea", style(tex) replace booktabs ///
	d(*) nolabel noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" )
}


*-------------------------------------------------------------
* Line-team level clustering
*-------------------------------------------------------------

if `line_clustering' == 1{
	
	
* Load Cleaned Data
use "$Data/Final/worker_choices.dta", clear

egen line_team = group(Line Team)


* Table B.20: Treatment effect on worker interactions in the paper


areg chosen mixed i.skill i.religion if Free, absorb(line_section) cluster(line_team)
eststo A1
local A1_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local A1_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local A1_stars = cond(`A1_pval' < 0.01, "***", cond(`A1_pval' < 0.05, "**", cond(`A1_pval' < 0.10, "*", "")))
local A1_result = "`A1_coef'`A1_stars'"

local obs1 = e(N)
su chosen if e(sample) == 1
local meanA = string(round(r(mean), .01))

wildbootstrap areg chosen mixed i.skill i.religion if Free, absorb(line_section) cluster(line_team) rseed(12345) coefficients(mixed)
local A1_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")


areg chosen mixedXLD mixedXHD i.skill i.religion if Free, absorb(line_section) cluster(line_team)
eststo A2
local LD_A2_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local LD_A2_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local LD_A2_stars = cond(`LD_A2_pval' < 0.01, "***", cond(`LD_A2_pval' < 0.05, "**", cond(`LD_A2_pval' < 0.10, "*", "")))
local LD_A2_result = "`LD_A2_coef'`LD_A2_stars'"

local HD_A2_pval = string(round(r(table)[4,2], 0.001), "%9.3f")
local HD_A2_coef = string(round(r(table)[1,2], 0.001), "%9.3f")
local HD_A2_stars = cond(`HD_A2_pval' < 0.01, "***", cond(`HD_A2_pval' < 0.05, "**", cond(`HD_A2_pval' < 0.10, "*", "")))
local HD_A2_result = "`HD_A2_coef'`HD_A2_stars'"

local obs2 = e(N)
test mixedXLD = mixedXHD
local p1 = string(round(r(p), .01))

wildbootstrap areg chosen mixedXLD mixedXHD i.skill i.religion if Free, absorb(line_section) cluster(line_team) rseed(29167) coefficients(mixedXLD mixedXHD)
local LD_A2_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")
local HD_A2_pval_wbs = string(round(r(table)[3,2], 0.001), "%9.3f")


areg chosen mixed i.skill i.religion if Blame, absorb(line_section) cluster(line_team)
eststo A3
local A3_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local A3_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local A3_stars = cond(`A3_pval' < 0.01, "***", cond(`A3_pval' < 0.05, "**", cond(`A3_pval' < 0.10, "*", "")))
local A3_result = "`A3_coef'`A3_stars'"

local obs3 = e(N)
su chosen if e(sample) == 1
local meanB = string(round(r(mean), .01))

wildbootstrap areg chosen mixed i.skill i.religion if Blame, absorb(line_section) cluster(line_team) rseed(12345) coefficients(mixed)
local A3_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")

areg chosen mixedXLD mixedXHD i.skill i.religion if Blame, absorb(line_section) cluster(line_team)
eststo A4
local LD_A4_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local LD_A4_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local LD_A4_stars = cond(`LD_A4_pval' < 0.01, "***", cond(`LD_A4_pval' < 0.05, "**", cond(`LD_A4_pval' < 0.10, "*", "")))
local LD_A4_result = "`LD_A4_coef'`LD_A4_stars'"
 
local HD_A4_pval = string(round(r(table)[4,2], 0.001), "%9.3f")
local HD_A4_coef = string(round(r(table)[1,2], 0.001), "%9.3f")
local HD_A4_stars = cond(`HD_A4_pval' < 0.01, "***", cond(`HD_A4_pval' < 0.05, "**", cond(`HD_A4_pval' < 0.10, "*", "")))
local HD_A4_result = "`HD_A4_coef'`HD_A4_stars'"

local obs4 = e(N)
test mixedXLD = mixedXHD
local p2= string(round(r(p), .01))

wildbootstrap areg chosen mixedXLD mixedXHD i.skill i.religion if Blame, absorb(line_section) cluster(line_team) rseed(99312) coefficients(mixedXLD mixedXHD)
local LD_A4_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")
local HD_A4_pval_wbs = string(round(r(table)[3,2], 0.001), "%9.3f")


areg chosen mixed i.skill i.religion if Choice_R, absorb(line_section) cluster(line_team)
eststo A5
local A5_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local A5_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local A5_stars = cond(`A5_pval' < 0.01, "***", cond(`A5_pval' < 0.05, "**", cond(`A5_pval' < 0.10, "*", "")))
local A5_result = "`A5_coef'`A5_stars'"

local obs5 = e(N)
su chosen if e(sample) == 1
local meanC = string(round(r(mean), .01))

wildbootstrap areg chosen mixed i.skill i.religion if Choice_R, absorb(line_section) cluster(line_team) coefficients(mixed) rseed(12345)
local A5_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")


areg chosen mixedXLD mixedXHD i.skill i.religion if Choice_R, absorb(line_section) cluster(line_team)
eststo A6
local LD_A6_pval = string(round(r(table)[4,1], 0.001), "%9.3f")
local LD_A6_coef = string(round(r(table)[1,1], 0.001), "%9.3f")
local LD_A6_stars = cond(`LD_A6_pval' < 0.01, "***", cond(`LD_A6_pval' < 0.05, "**", cond(`LD_A6_pval' < 0.10, "*", "")))
local LD_A6_result = "`LD_A6_coef'`LD_A6_stars'"
 
local HD_A6_pval = string(round(r(table)[4,2], 0.001), "%9.3f")
local HD_A6_coef = string(round(r(table)[1,2], 0.001), "%9.3f")
local HD_A6_stars = cond(`HD_A6_pval' < 0.01, "***", cond(`HD_A6_pval' < 0.05, "**", cond(`HD_A6_pval' < 0.10, "*", "")))
local HD_A6_result = "`HD_A6_coef'`HD_A6_stars'"

local obs6 = e(N)
test mixedXLD = mixedXHD
local p3 = string(round(r(p), .01))

wildbootstrap areg chosen mixedXLD mixedXHD i.skill i.religion if Choice_R, absorb(line_section) cluster(line_team) coefficients(mixedXLD mixedXHD) rseed(89542)
local LD_A6_pval_wbs = string(round(r(table)[3,1], 0.001), "%9.3f")
local HD_A6_pval_wbs = string(round(r(table)[3,2], 0.001), "%9.3f")


* Open a .tex file to store the results
file open results using $Output/Tables/tables_wchoices2, write replace

* Write the results to the .tex file
file write results "Mixed & `A1_result' &  & `A3_result' &  & `A5_result' & \\" _n
file write results "  & (`A1_pval') &  & (`A3_pval') &  & (`A5_pval') & \\" _n
file write results "  & [`A1_pval_wbs'] &  & [`A3_pval_wbs'] &  & [`A5_pval_wbs'] & \\" _n
file write results "Mixed $\times$ LD &  & `LD_A2_result' &  & `LD_A4_result' &  & `LD_A6_result' \\" _n
file write results "  &  & (`LD_A2_pval') &  & (`LD_A4_pval') &  & (`LD_A6_pval')  \\" _n
file write results "  &  & [`LD_A2_pval_wbs'] &  & [`LD_A4_pval_wbs'] &  & [`LD_A6_pval_wbs'] \\" _n
file write results "Mixed $\times$ HD &  & `HD_A2_result' &  & `HD_A4_result' &  & `HD_A6_result'  \\" _n
file write results "  &  & (`HD_A2_pval') &  & (`HD_A4_pval') &  & (`HD_A6_pval')  \\" _n
file write results "  &  & [`HD_A2_pval_wbs'] &  & [`HD_A4_pval_wbs'] &  & [`HD_A6_pval_wbs'] \\" _n
file close results
		
				
local tex " \\ \hline"
local tex "`tex' p(Mixed $\times$ HD = Mixed $\times$ LD) &  & `p1' &  & `p2' & & `p3'  \\"
local tex "`tex' Worker Skill F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Religion F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Line $\times$ Section F.E. & Yes & Yes & Yes & Yes & Yes & Yes \\"
local tex "`tex' Mean Dep Var. & `meanA' & `meanA' & `meanB' & `meanB' & `meanC' & `meanC'   \\"
local tex "`tex' N & `obs1' & `obs2' & `obs3' & `obs4'  & `obs5' & `obs6'   \\"
local tex "`tex' \multicolumn{7}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at line-section-team level."
local tex "`tex' Mixed  is  a  dummy  variable  coded  1  if  the  line-section-level  team  is  religiously mixed." 
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 A2 A3 A4 A5 A6 using "$Output/Tables/tables_wchoices2a", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" ) ///
	mgroups("Identified teammate as Free Rider" "Blamed by teammate" "Unwilling to give up Relief Time", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	
}

